package com.wbao.dianniu.adapter;

import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.data.WorkExperienceData;
import com.wbao.dianniu.listener.IJobWorkDelListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.JobWorkDelManager;
import com.wbao.dianniu.update.WorkListChangeManager;
import com.wbao.dianniu.ui.AddJobExActivity;
import com.wbao.dianniu.utils.Notification;

/**
 * 工作经历列表
 *
 * @author yejianjun
 */
public class WorkExperienceAdapter extends BaseAdapter implements IJobWorkDelListener {
    private LayoutInflater mInflater;
    private List<WorkExperienceData> mList;
    private int type;
    private final int TYPE_LIST = 1;
    private final int TYPE_DELETE = 2;
    private Context mContext;
    private JobWorkDelManager manager;
    private int index;

    public WorkExperienceAdapter(Context context, int type) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.type = type;
        manager = new JobWorkDelManager(mContext);
        manager.addJobWorkDelListener(this);
    }

    public void setData(List<WorkExperienceData> list) {
        mList = list;
    }

    public List<WorkExperienceData> getList() {
        return mList;
    }

    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.work_experience_list_item, null);
            holder = new GridHolder();
            holder.timeTV = convertView.findViewById(R.id.work_experience_time);
            holder.postTV = convertView.findViewById(R.id.work_experience_post);
            holder.contentTV = convertView.findViewById(R.id.work_experience_content);
            holder.Rl = convertView.findViewById(R.id.work_experience_list_rl);
            holder.editTV = convertView.findViewById(R.id.experience_edit);
            holder.deleteTV = convertView.findViewById(R.id.experience_delete);
            holder.line = convertView.findViewById(R.id.work_experience_line);
            convertView.setTag(holder);
        } else {
            holder = (GridHolder) convertView.getTag();
        }
        if (TYPE_LIST == type) {
            holder.Rl.setVisibility(View.GONE);
            holder.line.setVisibility(View.GONE);
        } else if (TYPE_DELETE == type) {
            holder.Rl.setVisibility(View.VISIBLE);
            holder.line.setVisibility(View.VISIBLE);
        }

        WorkExperienceData data = mList.get(position);
        if (null != data) {
            holder.timeTV.setText(mContext.getResources().getString(R.string.work_time) + data.getStartDate() + " 至 " + data.getEndDate());
            holder.postTV.setText(mContext.getResources().getString(R.string.work_post) + (data.getPost() == null ? "" : "" + data.getPost()));
            holder.contentTV.setText(mContext.getResources().getString(R.string.work_content) + (data.getContent() == null ? "" : "" + data.getContent()));
            holder.editTV.setOnClickListener(new MyClickListener(data, position));
            holder.deleteTV.setOnClickListener(new MyClickListener(data, position));
        }
        return convertView;
    }

    class MyClickListener implements OnClickListener {
        private int position;
        private WorkExperienceData data;

        public MyClickListener(WorkExperienceData data, int position) {
            // TODO Auto-generated constructor stub
            this.data = data;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.experience_edit:
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putInt(Const.BUNDLE_ID, data.getId());
                    bundle.putString(Const.BUNDLE_STARTDATE, data.getStartDate());
                    bundle.putString(Const.BUNDLE_ENDDATE, data.getEndDate());
                    bundle.putString(Const.BUNDLE_POST, data.getPost());
                    bundle.putString(Const.BUNDLE_CONTENT, data.getContent());
                    intent.putExtra(Const.BUNDLE_EDIT, bundle);
                    intent.setClass(mContext, AddJobExActivity.class);
                    mContext.startActivity(intent);
                    break;
                case R.id.experience_delete:
                    CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
                    builder.setTitle("确认删除该条工作经历吗？");
                    builder.setPositiveButton(mContext.getResources().getString(R.string.confirm),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    manager.jobWorkDel(data.getId(), data.getJobId());
                                    index = position;
                                }
                            });
                    builder.setNegativeButton(mContext.getResources().getString(R.string.cancel),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    builder.create().show();
                    break;
                default:
                    break;
            }
        }

    }

    private class GridHolder {
        public TextView timeTV;
        public TextView postTV;
        public TextView contentTV;
        public RelativeLayout Rl;
        public TextView editTV;
        public TextView deleteTV;
        private View line;
    }

    @Override
    public void onJobWorkDelFailure(int errorCode, String errorMsg) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onJobWorkDelSuccess() {
        // TODO Auto-generated method stub
        WorkListChangeManager workListChangeManager = WorkListChangeManager.getInstance();
        workListChangeManager.notifyRemove(index);
        Notification.toast(mContext, "删除成功");
    }

}
